var REPORT_PATH_PREFIX = "";
function setPathPrefix(path){
	REPORT_PATH_PREFIX =  path;
}
$(function() {
	//把访问路径传到js
	setPathPrefix(reportPath);
	$('#reportType').select2({
		placeholder : "报告类型"
	});
	$('#instId').select2({
		placeholder : "所属机构"
	});
	WASP_WIDGET.triggerDateRangeStyle("q-uploadTime-range");
	WASP_WIDGET.triggerDateRangeStyle("q-reportTime-range");
	WASP_WIDGET.triggerSelectOnDeptId("orgName",{multiple: false,leavedept: true});
	//项目经理
	WASP_WIDGET.triggerSelectOnEmpId("#prdManagerName");
	// 注册清空事件
    WASP_WIDGET.registerResetClearEvent();
	initData();
	chosenStyle();
});

//连接跳转过来也要选中样式
function chosenStyle(){
	var jgbg = window.parent.document.getElementById("jgbg");
	window.parent.pageApp.changeLocation($(tzzbg));
//    $(jgbg).addClass('active');
//    var gzhszl = window.parent.document.getElementById('gzhszl');
//    $(gzhszl).removeClass('active');
}

var $regulatoryReportList = $("#regulatoryReportList");

//需求待定，此处为临时写法
var instId = "1";

function initData(){

	$regulatoryReportList.jqGrid({
        url: REPORT_PATH_PREFIX+'/regulatoryReportListPage.do',
        /*caption: '监管报告列表',*/
        datatype: "local",
        colNames:["reportId","产品名称", "产品代码", "报告日期", "上传日期","监管机构","所属部门","项目经理", "报告类型","报告频率","操作"],        
		colModel:[
		          	{name:'reportId',index:'reportId',align:'left',key:true, hidden:true, resizable:true,sortable:false},
		          	{name:'prdSName',index:'prdSName',align:'left',width: 120	, hidden:false, resizable:true,sortable:false},
					{name:'prdCode',index:'prdCode',align:'left',width: 120	, hidden:false, sortable:false },
					{name:'reportDate',index:'reportDate',align:'left', hidden:false, resizable:true,sortable:false},
					{name:'uploadDate',index:'uploadDate',align:'left',hidden:false, resizable:true,sortable:false},
					{name:'instSName',index:'instSName',align:'left',width: 120, hidden:false, resizable:true,sortable:false},
					{name:'departName',index:'departName',align:'left',width: 120, hidden:false, resizable:true,sortable:false},
					{name:'managerName',index:'managerName',idth: 60,align:'left', hidden:false, resizable:true,sortable:false},
					{name:'reportTypeName',index:'reportTypeName',align:'left',width: 140, hidden:false, resizable:true,sortable:false},
					{name:'reportFrequencyName',index:'reportFrequencyName',align:'left',  width: 80,sortable:false},
					{name:'option',index:'option', width: 200, resizable:true, align:'left', sortable:false}
				],  
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: { search: "search", page: "pageNo", rows: "limit" },
        height: 'auto',
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        postData:{
        	'sp[instId]': instId
		},
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },
        multiselect: true,
        pager: "#regulatoryReportPage",
        hidegrid: false,
        gridComplete: function(){
        	var ids = $regulatoryReportList.jqGrid('getDataIDs');
	        	for(var i=0;i < ids.length;i++){
	        	var rowId = ids[i];
				var params = {'down' : 'regulatoryReportDwnListTemp'};
				var permissionButton = Permission_Button.configPermissionButton(params);
				permissionButton.down = Permission_Button.clickPermissionButton('regulatoryReportDwnListTemp',"down('" + rowId + "');");
				$regulatoryReportList.jqGrid('setRowData', ids[i], { option: Permission_Button.linkAllBtn(permissionButton)});
        	}
        }
	});

	$regulatoryReportList.navGrid('#regulatoryReportPage', {
		edit : false,
		add : false,
		del : false,
		search : false,
		refreshstate: 'current' 
	});

	$regulatoryReportList.jqGrid('setFrozenColumns');
	jqGridResize($regulatoryReportList);
	$regulatoryReportList.jqGrid('setGridParam',{datatype:'json'});//初始的时候不查询
	json?queryByParamater(json):queryByCondtion(true);//判断是否来自首页
}

function queryByParamater(json){
	var obj = JSON.parse(json);
	$("#q-reportTime-range").val(obj.reportTimeBeg + " - " + obj.reportTimeEnd);
	var postData = $regulatoryReportList.jqGrid("getGridParam", "postData");
	$.extend(postData, {
        'sp[reprotDateBgn]': obj.reportTimeBeg,
        'sp[reprotDateEnd]': obj.reportTimeEnd
    });
	$regulatoryReportList.trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
}

function queryByCondtion(flag) {
	var postData = $regulatoryReportList.jqGrid("getGridParam", "postData");
	var prdname = $("#prdname").val().trim();
	var prdcode = $("#prdcode").val().trim();
	var orgName = $("#orgName").val();
	var prdManagerId = $("#prdManagerName").val();
	/*var instId = $("#instId").val();*/
	var reportType = $("#reportType").val();
    $.extend(postData, {
        'sp[prdname]':prdname,
        'sp[prdcode]':prdcode,
        'sp[orgName]':orgName,
        'sp[prdManagerId]':prdManagerId,
        'sp[instId]':instId,
        'sp[reportType]':reportType,
        'sp[uploadDateBgn]': $("#q-uploadTime-range").getDateRangeValue()[0],
		'sp[uploadDateEnd]': $("#q-uploadTime-range").getDateRangeValue()[1],
		'sp[reprotDateBgn]': $("#q-reportTime-range").getDateRangeValue()[0],
		'sp[reprotDateEnd]': $("#q-reportTime-range").getDateRangeValue()[1]
    });
    if (flag) {
    	$regulatoryReportList.trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
    } else {
    	$regulatoryReportList.trigger("reloadGrid"); //重新载入Grid表格
    }
};

function changeTab(changeInstId){
	instId = changeInstId;
	/*$("#instId").val(instId).select2();*/
	queryByCondtion(true);
}

function regulatoryReportAdd(){
	var actionUrl = REPORT_PATH_PREFIX + "/regulatoryReportAdd.do";
	openDialogNew(actionUrl);
}

function batchDown(){
	var ids = $regulatoryReportList.jqGrid('getGridParam', 'selarrrow');//选择多选
	if(ids.length == 0){
		ctools.alert('请至少选择一项!',"","warning");
		return;
	}
	var actionUrl = encodeURI(REPORT_PATH_PREFIX+"/regulatoryReportBatchDown.do?sp[ids]="+ ids.join(","));
    window.location = actionUrl;  
}

function down(rowid){
	var actionUrl = encodeURI(REPORT_PATH_PREFIX+"/regulatoryReportBatchDown.do?sp[ids]="+ rowid);
    window.location = actionUrl;  
}